#pragma once

// 18.有两个循环单链表，链表头指针分别为h1和h2，编写一个函数将链表h2链接到链表
// h1之后，要求链接后的链表仍保持循环链表形式。

// #include "vector"
#include "list"
#include "iostream"
#include "memory"
#include "assert.h"
// #define mallocFor(Type) malloc(sizeof())
// template <

// template <typename ElemType>
// ElemType *mallocListNode();

template <typename ElemType>
struct ListNode
{
	ElemType value;
	// std::shared_ptr<ListNode<ElemType>> next;
	ListNode<ElemType> *next;

	ListNode<ElemType> *last;
	// ListNode() {}

	//辅助检查代码问题
	~ListNode()
	{
		std::cout << "node value " << value << " destroyed\r\n";
	}
	/**
	 * 仅限head调用
	*/
	void print2End()
	{
		ListNode<ElemType> *curNode = this->next;
	loop:
		if (curNode != this)
		{
			std::cout << curNode->value << ",";
			curNode = curNode->next;
			goto loop;
		}
		// if (curNode->next)
		// {

		// }
		std::cout << std::endl;
		//while()
	}
};

//无头节点链表构造
template <typename ElemType>
void initListByArr(ListNode<ElemType> *head, ElemType arr[], size_t arrLen)
{
	ListNode<ElemType> *curNode = head;

	//value = arr[0];
	// curNode->value = arr[0];
	for (int i = 0; i < arrLen; i++)
	{
		curNode->next = mallocListNode<ElemType>(); //(ListNode<ElemType> *)malloc(sizeof(ListNode<ElemType>)); //std::make_shared<ListNode<ElemType>>();
		curNode->next->last = curNode;
		curNode = curNode->next;
		curNode->value = arr[i];
	}
	curNode->next = head;
	head->last = curNode;
}

template <typename ElemType>
ListNode<ElemType> *mallocListNode()
{
	auto ret = (ListNode<ElemType> *)malloc(sizeof(ListNode<ElemType>));
	ret->next = NULL;
	ret->last = NULL;
	return ret;
	// ret
}

template <typename ElemType>
void connectListB2A(ListNode<ElemType> *headA, ListNode<ElemType> *headB)
{
	auto cur = headA->next;
	while (cur->next)
	{

		cur = cur->next;
	}
	cur->next = headB->next;
}

template <typename ElemType>
void combine_1_2(ListNode<ElemType> *head1, ListNode<ElemType> *head2)
{
	auto cur1 = head1;
	auto cur2 = head2;

	while (cur1->next != head1)
	{
		cur1 = cur1->next;
	}
	cur1->next = head2->next;
	// cur1 = cur1->next;
	while (cur1->next != head2)
	{
		cur1 = cur1->next;
	}
	cur1->next = head1;

	// return true;
}

int main()
{
	std::cout << "hello \r\n";
	int arrA[] = {
		1, 2, 3, 4, 5, 5, 4, 3, 2, 1

	};
	int arrD[] = {
		5, 6, 7, 7, 7, 8, 9, 10, 10

	};
	int arrB[] = {-25, -10, 10, 11};
	int arrC[] = {2, 9, 17, 30, 41};

	//ListNode<int> a(arrA, sizeof(arrA) / sizeof(int));
	auto head = mallocListNode<int>(); //std::make_shared<ListNode<int>>(arrA, sizeof(arrA) / sizeof(int));
	initListByArr(head, arrA, sizeof(arrA) / sizeof(int));

	auto head2 = mallocListNode<int>(); //std::make_shared<ListNode<int>>(arrA, sizeof(arrA) / sizeof(int));
	initListByArr(head2, arrD, sizeof(arrD) / sizeof(int));

	combine_1_2(head, head2);
	// delSame(head);
	// auto ret = if_1_has_2(head, head2);
	if (head)
	{
		head->print2End();
		// head2->print2End();
		// ret->next->print2End();
		// std::cout << "has" << std::endl;
	}
	else
	{
		std::cout << "not found" << std::endl;
	}
	//a.print2End();
	// std::vector<int> A =
	////     std::vector<int> B =
	//std::vector<int> A(arrA, arrA + sizeof(arrA) / sizeof(int));
	//// std::vector<int> B(arrB, arrB + sizeof(arrB) / sizeof(int));
	//// std::vector<int> C(arrC, arrC + sizeof(arrC) / sizeof(int));

	//printVec(A);
	//// printVec(B);
	//// printVec(C);
	//quickSortStart(A);
	//printVec(A);
	// std::vector<int> vec1; // vec2, vec3;
	// int arr[100];
	// std::cout << "hello"
	//           << "\r\n";
	// //init data
	// for (int i = 0; i < 100; i++)
	// {
	//     arr[i] = i;
	// }
	// leftMoveArr(arr, 100, 30);

	// // fastFind(vec1, 9);
	// for (int i = 0; i < 100; i++)
	// {
	//     std::cout << arr[i] << ",";
	// }
	// std::cout << "\r\n";
	// for (int i = 0; i < vec2.size(); i++)
	// {
	//     std::cout << vec2[i] << ",";
	// }
	// std::cout << "\r\n";
	// ///////////////////////////////////////////////////
	// combineAB2C(vec1, vec2, vec3);
	// for (int i = 0; i < vec3.size(); i++)
	// {
	//     std::cout << vec3[i] << ",";
	// }
	// std::cout << "\r\n";
	return 0;
}